/*    This file is part of libmissive.
 *
 *    libmissive is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    libmissive is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with libmissive.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "box.h"

int
msg_box(Msg *msg, const Nonce nonce,
	const Box_skey my_skey, const Box_pkey their_pkey)
{
	size_t box_len = msg->len + crypto_box_MACBYTES;
	unsigned char *boxed = malloc(box_len);

	if (!boxed)
		return -1;

	if (crypto_box_easy(boxed, msg->buf, msg->len,
			    nonce, their_pkey, my_skey)) {
		free(boxed);
		return -1;
	}

	msg_dispose(msg);
	msg_send_init(msg, box_len, boxed, 1);
	return 0;
}

int
msg_unbox(Msg *msg, const Nonce nonce,
	  const Box_skey my_skey, const Box_pkey their_pkey)
{
	ssize_t unbox_len = (ssize_t) msg->len - (ssize_t) crypto_box_MACBYTES;
	unsigned char *unboxed;

	if (unbox_len < 0)
		return -1;

	if (!(unboxed = malloc(unbox_len)))
		return -1;

	if (crypto_box_open_easy(unboxed, msg->buf, msg->len,
				 nonce, their_pkey, my_skey)) {
		free(unboxed);
		return -1;
	}

	msg_dispose(msg);
	msg_send_init(msg, unbox_len, unboxed, 1);
	return 0;
}

int
boxinfo_add(Msg *msg, const Nonce nonce, const Box_pkey my_pkey)
{
	size_t len = msg->len + sizeof(Nonce) + sizeof(Box_pkey);
        unsigned char *buf = realloc(msg->buf, len);

	if (!buf)
		return -1;

	memcpy(buf + msg->len, nonce, sizeof(Nonce));
	memcpy(buf + msg->len + sizeof(Nonce), my_pkey, sizeof(Box_pkey));
	msg->buf = buf;
	msg->len = len;
	return 0;
}

int
boxinfo_remove(Msg *msg, Nonce nonce, Box_pkey their_pkey)
{
	if (boxinfo_get(msg, nonce, their_pkey) < 0)
		return -1;

	msg->len -= sizeof(Nonce) + sizeof(Box_pkey);
	return 0;
}

int
boxinfo_get(Msg *msg, Nonce nonce, Box_pkey their_pkey)
{
	if (msg->len < sizeof(Nonce) + sizeof(Box_pkey))
		return -1;

	if (nonce)
		memcpy(nonce,
		       (char *) msg->buf + msg->len -
		       (sizeof(Box_pkey) + sizeof(Nonce)),
		       sizeof(Nonce));

	if (their_pkey)
		memcpy(their_pkey,
		       (char *) msg->buf + msg->len - sizeof(Box_pkey),
		       sizeof(Box_pkey));

	return 0;
}

int
boxinfo_box(Msg *msg, const Nonce nonce, const Box_skey my_skey,
	    const Box_pkey my_pkey, const Box_pkey their_pkey)
{
	Err err;
	Msg copy;

	if (msg_copy(&copy, msg, &err) < 0)
		return -1;

	if (msg_box(&copy, nonce, my_skey, their_pkey) < 0 ||
	    boxinfo_add(&copy, nonce, my_pkey) < 0) {
		msg_dispose(&copy);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}

int
boxinfo_unbox(Msg *msg, Nonce nonce,
	      const Box_skey my_skey, Box_pkey their_pkey)
{
	Err err;
	Msg copy;
	Nonce tmp_nonce;
	Box_pkey tmp_pkey;

	if (msg_copy(&copy, msg, &err) < 0)
		return -1;

	if (!nonce)
		nonce = tmp_nonce;

	if (!their_pkey)
		their_pkey = tmp_pkey;

	if (boxinfo_remove(&copy, nonce, their_pkey) < 0 ||
	    boxinfo_unbox(&copy, nonce, my_skey, their_pkey) < 0) {
		msg_dispose(&copy);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}
